package com.amumu.netctoss.dao.impl;

import java.util.List;

import javax.annotation.Resource;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

public abstract class HibernateBaseDao extends HibernateDaoSupport {
	@Resource
	public void setMySessionFactory(SessionFactory sf) {
		super.setSessionFactory(sf);
	}

	/**
	 * 查询唯一对象
	 */
	public Object uniqueResult(String hql, Object... objects) {
		List list = getHibernateTemplate().find(hql, objects);
		return list.isEmpty() ? null : list.get(0);
	}

	/**
	 * 分页查询
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List findByPage(final int page, final int size, final String hql, final Object... objects) {
		return (List) getHibernateTemplate().execute(new HibernateCallback() {
			public List doInHibernate(Session session) throws HibernateException {
				Query query = session.createQuery(hql);
				if (objects != null) {
					for (int i = 0; i < objects.length; i++) {
						query.setParameter(i, objects[i]);
					}
				}
				query.setFirstResult((page - 1) * size);
				query.setMaxResults(size);
				return query.list();
			}
		});
	}

	/**
	 * SQLS查询
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List findByPageSQL(final int page, final int size, final String sql, final Object... objects) {
		return (List) getHibernateTemplate().execute(new HibernateCallback() {
			public List doInHibernate(Session session) throws HibernateException {
				NativeQuery query = session.createNativeQuery(sql);
				if (objects != null) {
					for (int i = 0; i < objects.length; i++) {
						query.setParameter(i, objects[i]);
					}
				}
				query.setFirstResult((page - 1) * size);
				query.setMaxResults(size);
				return query.list();
			}
		});
	}

	/**
	 * 执行DB操作
	 * 
	 * @param hql
	 * @param objects
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public int executeUpdate(final String hql, final Object... objects) {
		Object obj = getHibernateTemplate().execute(new HibernateCallback() {

			public Object doInHibernate(Session session) throws HibernateException {
				Query query = session.createQuery(hql);
				if (objects != null) {
					for (int i = 0; i < objects.length; i++) {
						query.setParameter(i, objects[i]);
						// System.out.println(objects[i]);
					}
				}
				return query.executeUpdate();

			}
		});
		if (obj == null) {
			return -1;
		}
		return (Integer) obj;
	}

	/**
	 * 查询页数
	 */
	public int getTotalPage(int size, String hql, Object... objects) {
		List list = getHibernateTemplate().find(hql, objects);
		int count = Integer.parseInt(list.get(0).toString());
		return (count + size - 1) / size;
	}

	/**
	 * SQL查询页数
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public int getTotalPageSQL(final int size, final String sql, final Object... objects) {

		Object obj = getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException {
				NativeQuery query = session.createNativeQuery(sql);
				if (objects != null) {
					// System.out.println("length:"+objects.length);
					for (int i = 0; i < objects.length; i++) {
						query.setParameter(i, objects[i]);
					}
				}
				return query.uniqueResult();
			}
		});
		int count = Integer.parseInt(obj.toString());
		return (count + size - 1) / size;
	}
}
